<!-- 登录 -->
<template>
  <div
    class="login-container"
    @keyup.enter.native="handleLogin">
    <div class="login-weaper animated bounceInDown">
      <div class="login-left"/>
      <div class="login-border">
        <div class="login-main">
          <h4 class="login-title">
            励尔云欢迎您！
          </h4>
          <el-form
            ref="form"
            :rules="rules"
            :model="form"
            class="login-form"
            label-width="0">
            <el-form-item prop="mobile">
              <el-input
                v-model="form.mobile"
                placeholder="请输入手机号"
                auto-complete="off"
                @keyup.enter.native="handleLogin"/>
            </el-form-item>
            <el-form-item prop="code">
              <el-input
                v-model="form.code"
                placeholder="输入验证码"
                auto-complete="off"
                @keyup.enter.native="handleLogin"/>
              <span v-loading="verifyLoading" v-if="show" class="getCode" @click="sendShortMsg">获取验证码</span>
              <span v-if="!show" class="getCode">已发送({{ count }}s)</span>
            </el-form-item>
            <el-form-item>
              <el-button
                v-loading="loginSubmitLoading"
                type="primary"
                size="small"
                class="login-submit"
                @click.native.prevent="handleLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { sendMsg } from '@/api/login'
import { validateMobile } from '@/utils/validate'
const isMobile = (rule, value, callback) => {
  if (!validateMobile(value)) {
    callback(new Error('请输入正确的手机号码'))
  } else {
    callback()
  }
}

export default {
  name: 'Login',
  data() {
    return {
      form: {
        mobile: '18500613585',
        // mobile: '17621652421',
        // mobile: '17521272188',
        code: '1111'
      },
      rules: {
        mobile: [{
          required: true,
          message: '请输入手机号码',
          trigger: 'change'
        },
        {
          validator: isMobile,
          trigger: 'change'
        }],
        code: [{
          required: true,
          message: '请输入验证码',
          trigger: 'change'
        }]
      },
      loginSubmitLoading: false,
      passwordType: 'password',
      verifyLoading: false,
      show: true,
      count: '',
      timer: null
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    },
    'form.mobile': {
      handler(newVal) {
        console.log(newVal)
        var str = newVal
        this.form.mobile = str.replace(/\s+/g, '')
      },
      immediate: false,
      deep: true
    },
    'form.code': {
      handler(newVal) {
        var str = newVal
        this.form.code = str.replace(/\s+/g, '')
      },
      immediate: false,
      deep: true
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    sendShortMsg() {
      if (!this.show) {
        this.$notify.error({
          title: '失败',
          message: '请等待60s后重试'
        })
        return
      }
      if (this.form.mobile === '') {
        this.$notify.error({
          title: '失败',
          message: '请输入手机号码'
        })
        return false
      }
      if (!validateMobile(this.form.mobile)) {
        this.$notify.error({
          title: '失败',
          message: '请输入正确的手机号码'
        })
        return false
      }
      this.verifyLoading = true
      sendMsg(this.form.mobile)
        .then(response => {
          this.verifyLoading = false
          this.$notify.success({
            title: '成功',
            message: '信息发送成功'
          })
          const TIME_COUNT = 60
          if (!this.timer) {
            this.count = TIME_COUNT
            this.show = false
            this.timer = setInterval(() => {
              if (this.count > 0 && this.count <= TIME_COUNT) {
                this.count--
              } else {
                this.show = true
                clearInterval(this.timer)
                this.timer = null
              }
            }, 1000)
          }
        })
        .catch(response => {
          this.verifyLoading = false
        })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.form.validate(valid => {
        if (valid && !this.loginSubmitLoading) {
          this.loginSubmitLoading = true
          this.$store
            .dispatch('LoginByUsername', this.form)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
            })
            .catch(response => {
              this.loginSubmitLoading = false
            })
        } else {
          this.loginSubmitLoading = false
          return false
        }
      })
    }
  }
}
</script>

<style>
.msg-text {
  display: block;
  width: 60px;
  font-size: 12px;
  text-align: center;
  cursor: pointer;
}
.msg-text.display {
  color: #ccc;
}
</style>

<style lang="scss">
@import "@/styles/login.scss";
</style>
